﻿@model IEnumerable<LotteryVoteMVC.Models.CommissionValue>
@using LotteryVoteMVC.Core
@using LotteryVoteMVC.Models
@using LotteryVoteMVC.Resources
@using LotteryVoteMVC.Utility
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>EditUserComm</title>
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Customer/resource_" + CultureHelper.GetCurrentCulture() + ".js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Customer/comm.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Customer/ajaxHelper.js")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.theme.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/comm.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/TableBase.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/commission.css")" rel="stylesheet" type="text/css" />
    <style type="text/css">
        #setBoard input
        {
            margin: 2.5px;
            width: 50px;
        }
    </style>
    <script type="text/javascript">
        var showAllSetBoard = function (obj) {
            var setBoard = $("#setBoard");
            if (setBoard == null || setBoard.length == 0) {
                setBoard = $("<span id=\"setBoard\" style='width:110px'></span>");

                var comms = "0.1,0.5".split(",");
                $(comms).each(function (i, comm) {
                    var commButton = $("<input type='button' id='addComm{0}' name='addComm{0}' value='{1}' />".format(i, comms[i])).click(function () {
                        var parent = $("#" + $(this).parent().attr("target"));
                        var oldComm = parent.val();
                        var comm = parseFloat(oldComm) + parseFloat(comms[i]);
                        parent.val(comm.toFixed(1));
                    }).appendTo(setBoard);
                });

                var clear = $("<input type='button' id='clear' name='clear' value='{0}' />".format(resource.Clear)).click(function () {
                    var parent = $("#" + $(this).parent().attr("target"));
                    parent.val("0");
                }).appendTo(setBoard);

                var button = $("<input type='button' id='setAllEqual' value='All' />").bind("click", function () {
                    var val = $("#" + $(this).parent().attr("target")).val();
                    $(":text").val(val);
                    $("#setBoard").hide();
                }).appendTo(setBoard);

                setBoard.appendTo($("body"));
            }
            var offset = $(obj).offset();
            setBoard.attr("target", $(obj).attr("id")).css("left", offset.left + 60).css("top", offset.top).css("position", "absolute");
            setBoard.show();
        }

        $(document).ready(function () {
            $(":text").bind("click", function () {
                showAllSetBoard(this);
            }).numeral(true);
        });
    </script>
</head>
<body>
    <h3>@Resource.Commission</h3>
    @using (Html.BeginForm())
    {
        <table>
            <thead>
                <tr>
                    <td rowspan="2" class="rows">@Resource.GameType
                    </td>
                    @foreach (var comType in ViewBag.CompanyTypes)
                    { 
                        <td>@comType.Description
                        </td>
                    }
                </tr>
                <tr>
                    @for (int i = 0; i < ViewBag.CompanyTypes.Count; i++)
                    { 
                        <td>
                            @Resource.Comm
                        </td>
                    }
                </tr>
            </thead>
            @foreach (var gameType in ViewBag.GameTypes)
            {
                bool isABPL2 = gameType.Value == (int)GameType.A_B_PL2;
                bool isBCPL2 = gameType.Value == (int)GameType.B_C_PL2;
                bool isCAPL2 = gameType.Value == (int)GameType.C_A_PL2;
                <tr>
                    <td>@gameType.Description
                    </td>
                    @foreach (var comType in ViewBag.CompanyTypes)
                    {
                        var comm = Model.Find(it => it.GameId == gameType.Value && it.CompanyTypeId == comType.Value);
                        <td>
                        
                            @if (isABPL2 && comType.Value != (int)CompanyType.EighteenA)
                            {
                                WriteLiteral("");
                            }
                            else if (isBCPL2 && comType.Value != (int)CompanyType.EighteenB)
                            {
                                WriteLiteral("");
                            }
                            else if (isCAPL2 && comType.Value != (int)CompanyType.EighteenC)
                            {
                                WriteLiteral("");
                            }
                            else
                            {
                                using (Html.BeginCollectionItem("model"))
                                {
                                @Html.TextBox("Comm", comm == null ? "0" : comm.Comm.ToFormat(), new { @readOnly = "readOnly" })
                                @Html.ValidationMessage("Comm")
                                @Html.Hidden("GameId", (int)gameType.Value)
                                @Html.Hidden("CompanyTypeId", (int)comType.Value)
                                }
                            }
                        </td>
                    }
                </tr>
            }
        </table>
            if (this.HasRight((Role)ViewBag.Role - 1, "CommissionController", "EditUserComm"))
            {
        <div class="buttons">
            <input type="submit" name="submit" id="submit" value="@Resource.Submit"/>
            <input type="reset" name="reset" id="reset" value="@Resource.Reset" />
        </div>
            }
    }
    <h3>@Resource.Package</h3>
    <table class="outTable">
        <colgroup>
            <col style="background: #B3B3B3" />
        </colgroup>
        @foreach (var group in ViewBag.CommGroups)
        { 
            <tr>
                <td>
                    <span>
                        @group.Key.GroupName</span>
                </td>
                <td>
                    <table class="innerTable">
                        <thead>
                            <tr>
                                <td rowspan="2">
                                    @Resource.GameType
                                </td>
                                @foreach (var comType in ViewBag.CompanyTypes)
                                { 
                                    <td colspan="2">
                                        @comType.Description
                                    </td>
                                }
                            </tr>
                            <tr>
                                @for (int i = 0; i < ViewBag.CompanyTypes.Count; i++)
                                { 
                                    <td>@Resource.Comm
                                    </td>
                                    <td>@Resource.Odds
                                    </td>
                                }
                            </tr>
                        </thead>
                        @foreach (var gameType in ViewBag.GameTypes)
                        {
                            bool isABPL2 = gameType.Value == (int)GameType.A_B_PL2;
                            bool isBCPL2 = gameType.Value == (int)GameType.B_C_PL2;
                            bool isCAPL2 = gameType.Value == (int)GameType.C_A_PL2;
                            <tr>
                                <td>@gameType.Description
                                </td>
                                @foreach (var comType in ViewBag.CompanyTypes)
                                {
                                    if ((isABPL2 && comType.Value != (int)CompanyType.EighteenA) || (isBCPL2 && comType.Value != (int)CompanyType.EighteenB) || (isCAPL2 && comType.Value != (int)CompanyType.EighteenC))
                                    {
                                    <td>
                                    </td>
                                    <td>
                                    </td>
                                    }
                                    else
                                    {
                                        var comm = (group.Value as IEnumerable<ConcreteCommission>).Find(it => (int)it.GameType == gameType.Value && (int)it.CompanyType == comType.Value);
                                    <td>@(comm == null ? 0 : comm.Commission)
                                    </td>
                                    <td>@(comm == null ? 0 : comm.Odds)
                                    </td>
                                    }
                                }
                            </tr>
                        }
                    </table>
                </td>
            </tr>
        }
    </table>
</body>
</html>
